<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="queryForms">
			<el-form-item label="零件名称" prop="partName">
				<el-input maxlength="20" show-word-limit  v-model="queryParams.partName" placeholder="请输入零件名称" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="零件图号" prop="partFigure">
				<el-input maxlength="20" show-word-limit  v-model="queryParams.partFigure" placeholder="请输入零件图号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:productProcessStepPart:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['erp:productProcessStepPart:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button size="mini" type="info" @click="handleBacks">返回</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button size="mini">{{ $route.query.productName }}-{{ $route.query.productModel }}</el-button>
			</el-col>
			<right-toolbar @changeTableData="changeTableData" menuNames="productProcessStepPart33" :showSearch.sync="showSearch" @queryTable="getList" :columns.sync="columns"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table ref="table" @row-click="row_click" v-loading="loading" :data="productProcessStepPartList" @selection-change="handleSelectionChange" border height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" prop="id" width="55" type="index" />
				<el-table-column label="零组件名称" prop="partName" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="零组件图号" prop="partFigure" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<el-table-column label="单套数量" align="center" prop="partQuantity" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible">
					<template slot-scope="scope">
						<el-input v-model="scope.row.partQuantity" @change="updateData(scope.row)" maxlength="100"></el-input>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['erp:productProcessStepPart:remove']">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination37" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改产品工序步骤产品对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :before-close="handleClose" v-dialogDrag>
			<el-form :model="queryProduct" ref="queryProduct" size="small" :inline="true" label-width="85px">
				<el-form-item label="零组件名称" prop="partName">
					<el-input maxlength="20" show-word-limit  v-model="queryProduct.partName" placeholder="请输入零组件名称" clearable @keyup.enter.native="handleQueryProduct" />
				</el-form-item>
				<el-form-item label="零组件图号" prop="partFigure">
					<el-input maxlength="50" show-word-limit  v-model="queryProduct.partFigure" placeholder="请输入零组件图号" clearable @keyup.enter.native="handleQueryProduct" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryProduct">搜索</el-button>
					<el-button icon="el-icon-refresh" size="mini" @click="resetQueryProduct">重置</el-button>
				</el-form-item>
			</el-form>
			<el-table @select="handleSelectionProduct" @select-all="handleAllChange" v-loading="loadingproduct" :data="ProductList" ref="multipleTable" row-class-name="pointer">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55" />
				<el-table-column label="零组件名称" prop="partName" min-width="150" :show-overflow-tooltip="true" />
				<el-table-column label="零组件图号" prop="partFigure" min-width="150" :show-overflow-tooltip="true" />
			</el-table>
			<pagination v-show="totalproduct > 0" :total="totalproduct" :page.sync="queryProduct.pageNum" :limit.sync="queryProduct.pageSize" @pagination="getProduct" />
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { installPart, listProductProcessStepPart, getProductProcessStepPart, delProductProcessStepPart, addProductProcessStepPart, updateProductProcessStepPart } from "@/api/productTechnology/productProcessStepPart"
import { productProcessStepPart ,getcolumn} from "@/api/selectList"
import { listProductPart } from "@/api/product/productPart"
import { mapGetters } from "vuex"

export default {
	name: "productProcessStepPart",
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
      productId:'',
      processStepId:'',
			eleTabTop: 0, //表格容器距浏览器顶部距离
			// 产品列表
			ProductList: [],
			// 选中的产品
			products: [],
			// 回显的数据id
			echoList: [],
			selectRow: [],
			// 产品列表条数
			totalproduct: 0,
			loadingproduct: false,
			// 产品查询条件
			queryProduct: { pageNum: 1, pageSize: 10, partName: null, partFigure: null, state: "1" },
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 产品工序步骤产品表格数据
			productProcessStepPartList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				processStepId: null,
				productPartId: null,
				partName: null,
				partFigure: null,
				partQuantity: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
			// 列信息
			columns: [
				{ key: 0, label: `零组件名称`, visible: true },
				{ key: 1, label: `零组件图号`, visible: true },
				{ key: 2, label: `单套数量`, visible: true }
			]
		}
	},
	created() {

		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
    this.processStepId = this.$route.query.id
    this.productId = this.$route.query.productId
    this.getSize()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
    getSize() {
			getcolumn({ pageName: "Pagination37" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		// 单击表格行
		row_click(row, column, event) {
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.table.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.table.doLayout()
			})
		},

		handleBacks() {
			const obj = { path: this.$route.path }
			this.$tab.closePage(obj).then(() => {
        this.$router.go(-1)
				// this.$router.push({
				// 	path: `/productTechnology/Technology/productProcessStep/index`,
				// 	query: {
				// 		productName: this.$route.query.productName,
				// 		productModel: this.$route.query.productModel
				// 	}
				// })
			})
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		updateData(row) {
			updateProductProcessStepPart(row).then(res => {
				this.$modal.msgSuccess("修改成功")
				this.getList()
			})
		},
		// 全选、取消全选
		handleAllChange(selecteds) {
			if (selecteds.length > 0) {
				selecteds.forEach(item => {
					if (!this.echoList.includes(item.id)) {
						this.echoList.push(item.id)
						this.selectRow.push(item)
					}
				})
			} else {
				this.ProductList.forEach(item => {
					this.echoList.forEach((id, index) => {
						if (id === item.id) {
							this.echoList.splice(index, 1)
							this.selectRow.splice(index, 1)
						}
					})
				})
			}
		},
		// 选择关联产品：打勾或取消
		handleSelectionProduct(selecteds, row) {
			if (!this.echoList.includes(row.id)) {
				// 回显数据里没有本条，把这条加进来(选中)
				this.echoList.push(row.id)
				this.selectRow.push(row)
			} else {
				// 回显数据里有本条，把这条删除(取消选中)
				this.echoList.forEach((id, index) => {
					if (id === row.id) {
						this.echoList.splice(index, 1)
						this.selectRow.splice(index, 1)
					}
				})
			}
		},
		// 点击dialog右上角关闭图标，隐藏dialog框时触发。
		handleClose(down) {
			this.$refs.multipleTable.clearSelection()
			down()
		},
		cancel() {
			this.$refs.multipleTable.clearSelection()
			this.open = false
		},
		submitForm() {
			this.selectRow.forEach(item => {
				item.productPartId = item.productPartId || item.id
				item.processStepId = this.processStepId
				delete item.id
			})
			installPart(this.selectRow).then(res => {
				this.$modal.msgSuccess("新增成功")
				this.getList()
				this.open = false
			})
		},
		// 查询新增弹框的列表
		getProduct() {
			this.loadingproduct = true
			this.queryProduct.productId = this.productId
			listProductPart(this.queryProduct).then(response => {
				this.ProductList = response.rows
				this.totalproduct = response.total
				this.loadingproduct = false
				this.$nextTick(() => {
					this.ProductList.forEach(item => {
						// 在当前分页列表中查询与回显数据是否有一致的id，一致则勾选数据回显
						// toggleRowSelection(item, true)：设置当前行数据为选中状态
						if (this.echoList.includes(item.id)) {
							this.$refs.multipleTable.toggleRowSelection(item, true)
						}
					})
				})
			})
		},
		resetQueryProduct() {
			this.resetForm("queryProduct")
			this.handleQueryProduct()
		},
		handleQueryProduct() {
			this.queryProduct.pageNum = 1
			this.getProduct()
		},
		/** 查询产品工序步骤产品列表 */
		getList() {
			this.loading = true
			this.queryParams.processStepId = this.processStepId
			listProductProcessStepPart(this.queryParams).then(response => {
				this.productProcessStepPartList = response.rows
				this.total = response.total
				this.loading = false
			})
		},
		// 表单重置
		reset() {
			this.form = {
				id: null,
				processStepId: this.processStepId,
				productPartId: null,
				partName: null,
				partFigure: null,
				partQuantity: null
			}
			this.resetForm("form")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset()
			Promise.all([this.getrepairProductList()]).then(res => {
				this.getProduct()
			})
			this.open = true
			this.title = "添加产品工序步骤产品"
		},
		getrepairProductList() {
			return new Promise((resolve, reject) => {
				productProcessStepPart({ processStepId: this.processStepId }).then(res => {
					// 筛选回显数据的id，生成数组，根据id来勾选数据
					this.echoList = res.rows.map(item => item.productPartId)
					this.selectRow = res.rows
					resolve()
				})
			})
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除产品工序步骤产品数据项？")
				.then(function () {
					return delProductProcessStepPart(ids)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				})
				.catch(() => {})
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset()
			const id = row.id || this.ids
			getProductProcessStepPart(id).then(response => {
				this.form = response.data
				this.open = true
				this.title = "修改产品工序步骤产品"
			})
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download(
				"erp/productProcessStepPart/export",
				{
					...this.queryParams
				},
				`productProcessStepPart_${new Date().getTime()}.xlsx`
			)
		}
	}
}
</script>
